package kiwi.assist.dev.guieditor.command {
	import fl.core.UIComponent;

	import kiwi.assist.dev.guieditor.event.GUIEditorEvent;
	import kiwi.assist.dev.guieditor.event.GUIOperateEvent;
	import kiwi.assist.dev.guieditor.model.GUIEditorConfig;
	import kiwi.assist.dev.guieditor.model.SkinConfig;
	import kiwi.assist.dev.guieditor.model.StyleLibConfig;
	import kiwi.assist.dev.guieditor.model.UiDetailVO;
	import kiwi.assist.dev.util.SkinComponentAdapter;
	import kiwi.util.ArrayUtils;

	import org.aswing.AsWingUtils;
	import org.aswing.GeneralListCellFactory;
	import org.aswing.JButton;
	import org.aswing.JFrame;
	import org.aswing.JLabel;
	import org.aswing.JList;
	import org.aswing.JPanel;
	import org.aswing.JScrollPane;
	import org.aswing.JTextField;
	import org.aswing.SoftBoxLayout;
	import org.aswing.event.ListItemEvent;
	import org.aswing.geom.IntPoint;
	import org.robotlegs.mvcs.Command;

	import flash.events.Event;
	import flash.events.MouseEvent;

	/**
	 * 显示一个背景选择对话框
	 * @author zhangming.luo
	 */
	public class ShowBackgroundSelectDialogCommand extends Command {
		[Inject]
		public var config : GUIEditorConfig;
		[Inject]
		public var detail : UiDetailVO;
		[Inject]
		public var event : GUIEditorEvent;
		private var frame : JFrame;
		private var content : JPanel;
		private var listPanel : JScrollPane;
		private var okButton : JButton;
		private var skinList : JList;
		private var component : SkinComponentAdapter;
		private var filterInput : JTextField;
		private var filter : String;
		private var all : Array;

		override public function execute() : void {
			super.execute();
			frame = new JFrame(contextView.stage, "选择背景", true);
			// 显示容器列表
			frame.setContentPane(content = new JPanel());
			content.setLayout(new SoftBoxLayout(SoftBoxLayout.Y_AXIS));
			// 添加一个过滤器
			var filterPanel : JPanel = new JPanel();
			filterPanel.append(new JLabel("过滤条件:"));
			filterPanel.append(filterInput = new JTextField());
			filterInput.setPreferredWidth(200);
			filterInput.addEventListener(Event.CHANGE, function() : void {
				filter = filterInput.getText();
				skinList.setListData(ArrayUtils.select(all, function(item : SkinConfig) : Boolean {
					return item.name.indexOf(filter.toUpperCase()) >= 0;
				}));
			});
			content.append(filterPanel);
			listPanel = new JScrollPane();
			skinList = new JList(null, new GeneralListCellFactory(IconListCell, false, false));
			// 添加背景
			all = [];
			for each (var lib:StyleLibConfig in config.styles) {
				for each (var skin:SkinConfig in lib.skines) {
					if (/^[A-Z0-9_]+$/.test(skin.name)) {
						all.push(skin);
					}
				}
			}
			all.sortOn("name");
			skinList.setListData(all);
			skinList.addEventListener(ListItemEvent.ITEM_DOUBLE_CLICK, function(e : ListItemEvent) : void {
				frame.closeReleased();
				select(e.getValue());
			});
			listPanel.append(skinList);
			listPanel.setPreferredHeight(300);
			listPanel.setPreferredWidth(300);
			content.append(listPanel);
			content.append(okButton = new JButton("确定"));
			okButton.addEventListener(MouseEvent.CLICK, function(event : MouseEvent) : void {
				frame.closeReleased();
				if (skinList.getSelectedValue()) {
					select(skinList.getSelectedValue());
				} else {
					select(new SkinConfig(filterInput.getText()));
				}
			});
			frame.pack();
			frame.setResizable(false);
			var location : IntPoint = AsWingUtils.getScreenCenterPosition();
			location.x = Math.round(location.x - frame.getWidth() / 2);
			location.y = Math.round(location.y - frame.getHeight() / 2);
			frame.setLocation(location);
			frame.show();
		}

		private function select(skin : SkinConfig) : void {
			component = new SkinComponentAdapter(skin, config.uiPrefix);
			if (event.data) {
				var ui : UIComponent = event.data;
				component.x = ui.x;
				component.y = ui.y;
				component.width = ui.width;
				component.height = ui.height;
				component.name = ui.name;
				dispatch(new GUIOperateEvent(GUIOperateEvent.BACKGROUND_REMOVE, ui));
			}
			dispatch(new GUIOperateEvent(GUIOperateEvent.BACKGROUND_ADD, component));
		}
	}
}
import ghostcat.util.ReflectUtil;
import ghostcat.util.core.ClassFactory;

import kiwi.util.DisplayObjectUtils;

import org.aswing.*;

class IconListCell extends DefaultListCell {
	public function IconListCell() {
		super();
	}

	override public function getCellComponent() : Component {
		var comp : Component = super.getCellComponent();
		DisplayObjectUtils.doubleClickEnabled(comp);
		return comp;
	}

	override public function setCellValue(value : *) : void {
		if (this.value != value) {
			this.value = value;

			getJLabel().setText(value.toString());
			getJLabel().setIcon(new AssetIcon(ClassFactory.apply(ReflectUtil.getClass(value.clz)), 100, 50, true));
		}
	}
}
